//
//  TestWebViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2017/4/21.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit
import WebKit
import WebViewJavascriptBridge

class TestWebViewController: BaseWebViewController {
	
	var bridge: WebViewJavascriptBridge?

	override func viewDidLoad() {
		super.viewDidLoad()

		
		self.urlString = "http://www.wangjiangtao.top/java2js2.html"

		WebViewJavascriptBridge.enableLogging()
		bridge = WebViewJavascriptBridge(forWebView: self.webView!)!
		bridge!.setWebViewDelegate(self)
		
		
		self.loadData()
		
		
//		DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) { [weak self] in
//			self?.outputHtml()
//		}
	}
	
//	override func loadData() {
//		
//		let path = Bundle.main.path(forResource: "java2js2", ofType: "html")
////		let html = try? String(contentsOfFile: path!, encoding: String.Encoding.utf8)
//		
//		let url = try? fileURLForBuggyWKWebView8(fileURL: NSURL(fileURLWithPath: path!))
//		
//		self.webView?.load(URLRequest(url: url as! URL))
////		if html != nil {
////			self.webView?.loadHTMLString(html!, baseURL: nil)
////		}
//	}

	/// JS To iOS
	func login() {

		bridge?.registerHandler("loginAction", handler: { (data, responseCallback) in
			DebugLog("JS 调用 iOS原生代码，并传值过来")

			let dict = data as! NSDictionary
			DebugLog(dict)
			DebugLog("调用登录方法")



			responseCallback!(dict)


			//			self.h5Login()
		})

	}

	/// iOS To JS
	func h5Login() {
		bridge?.callHandler("registerAction", data: "uid:123 pwd:321", responseCallback: { (responseData) in

			//			let dict = ["name" : "哇哈哈啊", "age" : "28"]
			DebugLog(responseData)
		})
	}
}
